package infod

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

// DomainOption 域构建参数
type DomainOption struct {
	InfoRepo      InfoRepo
	InfoOplogRepo InfoOplogRepo
}

// NewDomain 核心子域内容信息域
func NewDomain(p ddd.Domain, opt DomainOption) InfoDomain {
	d := &infoDomain{}
	d.Init(d, p, DomainID)
	initInfo(d, opt.InfoRepo)
	initInfoOplog(d, opt.InfoOplogRepo)
	return d
}

// InfoDomain 信息域
type InfoDomain interface {
	ddd.Domain
	InfoAggregateRoot() InfoAggregateRoot
	InfoOplogAggregateRoot() InfoOplogAggregateRoot
}
type infoDomain struct {
	ddd.BaseDomain
	infoAggregateRoot      InfoAggregateRoot
	infoOplogAggregateRoot InfoOplogAggregateRoot
}

func (i *infoDomain) InfoAggregateRoot() InfoAggregateRoot {
	if i.infoAggregateRoot != nil {
		return i.infoAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(DomainID)
	if r == nil {
		panic("没有初始化InfoAggregateRoot")
	}
	ir, ok := r.(InfoAggregateRoot)
	if !ok {
		panic("初始化InfoAggregateRoot，类型错误")
	}
	i.infoAggregateRoot = ir
	return ir

}
func (i *infoDomain) InfoOplogAggregateRoot() InfoOplogAggregateRoot {
	if i.infoOplogAggregateRoot != nil {
		return i.infoOplogAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(InfoOplogAgID)
	if r == nil {
		panic("没有初始化InfoOplogAggregateRoot")
	}
	ir, ok := r.(InfoOplogAggregateRoot)
	if !ok {
		panic("初始化InfoOplogAggregateRoot，类型错误")
	}
	i.infoOplogAggregateRoot = ir
	return ir

}
